Variables
Variables store information. Variables can store one of three types of data: numbers, strings, and booleans.
Type | Possible Values | Examples |
---|---|---|
Number | Any whole or decimal number | 1, 2.5, 3468900, -500 |
String | Any sequence of letters, numbers, and other characters enclosed in quotes. | "Hello", "β", "A complete sentence." |
Boolean | Either true or false | true, false |
Each variable has its own name. In t-Plot, variable names always start with a dollar sign ($
).
Declaring Variablesβ
Declaring a variable means telling the system that a variable exists, what it's used for, and what its initial value is.
To declare a variable, use the <<declare>>
command:
/// The player's name.
<<declare $playerName = "Player">>
/// The number of gold coins the player has.
<<declare $gold = 0>>
/// Is the dungeon door unlocked?
<<declare $doorUnlocked = false>>
If you add a comment with three slashes ///
above a variable declaration, tools like Visual Studio Code will display that description when the variable is used elsewhere.
For example:
/// Current day. Starts at 0 and ends at 3.
<<declare $day = 0>>
When you hover over this variable in Visual Studio Code, a pop-up with its description will appear.
Assigning Values to Variablesβ
You can assign a value to a variable using the <<set>>
command. In t-Plot, you use the =
symbol. For example:
<<set $greeting = "Hello, t-Plot!">>
This assigns the string "Hello, t-Plot!"
to the variable $greeting
.
Like node titles, variable names cannot contain spaces. The first character must be a letter, and variable names should consist of letters, numbers, and underscores.
Variables and Typesβ
Each variable can store only one type of data. Variables can change their value, but they cannot change their type.
The following code will work:
<<set $myCoolNumber = 7>>
<<set $myFantasticString = "wow, text!">>
// Change values:
<<set $myCoolNumber = 8>>
<<set $myFantasticString = "incredible!">>
However, the following code will not work:
<<set $myCoolNumber = 7>>
<<set $myFantasticString = "wow, text!">>
// This will NOT work because the data type cannot be changed:
<<set $myCoolNumber = "8">>
<<set $myFantasticString = 42>>
Variables and Expressionsβ
You can perform operations on the values stored in variables. For example, numbers can be multiplied, and boolean values can be used in logical operations (and, or). This is called an expression.
<<set $numberOfSidesInATriangle = 2 + 1>>
<<set $numberOfSidesInASquare = $numberOfSidesInATriangle + 1>>
All values in an expression must be of the same type. For example, the following code will not work:
// This will NOT work because you cannot add a string to a number:
<<set $broken = "hello" + 1>>
String concatenation (adding strings together) is not supported in t-Plot.
Logical Operatorsβ
t-Plot supports the following logical operators:
- Equality:
==
- Inequality:
!
- Greater than:
>
- Less than:
<
- Less than or equal:
<=
- Greater than or equal:
>=
- Logical "or":
or
- Logical "not":
not
- Logical "and":
and
Mathematical Operatorsβ
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Remainder:
%
- Parentheses:
(
and)
Order of Operationsβ
t-Plot follows the standard order of operations. When operators have the same precedence, the expression is evaluated from left to right.
- Parentheses
- Logical "not"
- Multiplication, division, remainder
- Addition, subtraction
- Comparisons (less than, greater than, equal to)
- Equality, inequality
- Logical "and", "or"
Using Variables in Messagesβ
To display the value of a variable within a message, place it inside curly braces {}
. The value of the variable will be displayed in place of the braces.
<<set $variableName = "a string value">>
The value of variableName is {$variableName}.
The output will look like this:
The value of variableName is a string value.